<script lang="ts" setup>
  import { reactive, ref, watch } from "vue"
  import { getDepartmentList,deleteDepartmentById } from "@/api/department"
  import { type FormInstance, ElMessage, ElMessageBox } from "element-plus"
  import { usePagination } from "@/hooks/usePagination"
  import useTable from '@/hooks/useTable'
  import handle from './components/handle.vue'

  const loading = ref<boolean>(false)
  const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
  const tableData = ref<any>([])
  const searchFormRef = ref<FormInstance | null>(null)
  const searchData = reactive<any>({})
  const activeRow = ref({})
  const dialogVisible = ref(false)

  const getTableData = () => {
    loading.value = true
    getDepartmentList({
      ...paginationData,
      ...searchData
    }).then((res: any) => {
      paginationData.total = res.totalCount
      tableData.value = res.data
    }).catch(() => {
      tableData.value = []
    }).finally(() => {
      loading.value = false
    })
  }

  const handleShow = (e: any) => {
    activeRow.value = e
    dialogVisible.value = true
  }

  const handleAdd = () => {
    activeRow.value = {}
    dialogVisible.value = true
  }

  const handleUpdate = (row: any) => {
    activeRow.value = row
    dialogVisible.value = true
  }

  const closeHandle = () => {
    dialogVisible.value = false
  }
  //操作完成
  const successHandle = () => {
    dialogVisible.value = false
    getTableData()
  }

  //搜索操作
  const handleSearch = () => {
    paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
  }

  //重置搜索表单
  const resetSearch = () => {
    searchFormRef.value?.resetFields()
    handleSearch()
  }
  const { handleoneDelete } = useTable({getList:getTableData})

  watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })

</script>

<template>
  <div class="app-container">
      <div class="title-wrapper">部门管理</div>
      <div class="page-main">
      <!-- <div v-permission="['cashier-consumption:list']" shadow="never" class="search-wrapper">
        <div class="search-main">
          <el-form ref="searchFormRef" label-width="90px" :inline="true" label-position="left" :model="searchData">
            <el-form-item prop="name" label="部门名称">
              <el-input v-model="searchData.name" placeholder="请输入" clearable/>
            </el-form-item>
          </el-form>
        </div>
        <div class="search-btn">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </div>
      </div> -->
      <div class="toolbar-wrapper">
        <el-button type="primary" v-permission="['department-add']" @click="handleAdd">新增</el-button>
      </div>
      <div v-loading="loading"  class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column sortable prop="name" label="名称"  />
          <el-table-column sortable prop="manageName" label="负责人"  />
          <el-table-column sortable prop="manageMobile" label="负责人联系方式"  />
          <el-table-column sortable prop="createTime" label="创建日期"  />
          <el-table-column fixed="right" label="操作" >
            <template #default="scope">
              <el-button type="primary" v-permission="['department-edit']" text size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" v-permission="['department-del']" text size="small" @click="handleoneDelete(deleteDepartmentById, scope.row.id, scope.row.name)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>

  <handle
    v-if="dialogVisible"
    :data="activeRow"
    @success="successHandle"
    @cancel="closeHandle"
    />
</template>

<style lang="scss" scoped>

</style>
